//
//  StarredChannelsTableViewCell.m
//  TestApplication
//
//  Created by Hanlingyu on 16/8/30.
//  Copyright © 2016年 ltq. All rights reserved.
//

#import "StarredChannelsTableViewCell.h"
#import "StarredChannelsCollectionViewCell.h"
#import "Masonry.h"
#import "TAMacro.h"
#import "HomeChannels.h"
#import "AllStarredChannelsCollectionViewCell.h"
#import "StarredChannelsViewController.h"
#import "ChannelsDetailViewController.h"
@interface StarredChannelsTableViewCell ()
<
    UICollectionViewDataSource,
    UICollectionViewDelegate,
    UIScrollViewDelegate,
    UICollectionViewDelegateFlowLayout
>

@property (nonatomic, strong) UICollectionView *starredChannelsCollectionView;
@property (nonatomic, strong) UICollectionViewFlowLayout *layout;
@property (nonatomic, strong) UIPageControl *pageControl;

@end


@implementation StarredChannelsTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createView];
    }
    return self;
}


- (void)createView {
    self.contentView.backgroundColor = [UIColor colorWithWhite:0.917 alpha:1.000];
    _layout = [[UICollectionViewFlowLayout alloc] init];
    _layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    _layout.minimumLineSpacing = 20;
    _layout.minimumInteritemSpacing = 9;
    _starredChannelsCollectionView = [[UICollectionView alloc] initWithFrame:self.contentView.bounds collectionViewLayout:_layout];
    _starredChannelsCollectionView.pagingEnabled = YES;
    _starredChannelsCollectionView.backgroundColor = [UIColor colorWithWhite:0.928 alpha:1.000];
    [_starredChannelsCollectionView registerNib:[UINib nibWithNibName:@"StarredChannelsCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"homeCollectionViewCellReuse"];
    [_starredChannelsCollectionView registerNib:[UINib nibWithNibName:@"AllStarredChannelsCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"allChannelsCollectionViewCellReuse"];
    _starredChannelsCollectionView.delegate = self;
    _starredChannelsCollectionView.dataSource = self;
    [self.contentView addSubview:_starredChannelsCollectionView];
}

- (void)createPageControl {
    _pageControl = [[UIPageControl alloc] init];
    _pageControl.backgroundColor = [UIColor colorWithWhite:0.917 alpha:1];
    _pageControl.numberOfPages = 2;
    [self.contentView addSubview:_pageControl];
    _pageControl.pageIndicatorTintColor = [UIColor colorWithWhite:0.8 alpha:1];
    _pageControl.currentPageIndicatorTintColor = [UIColor grayColor];
}



- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    _pageControl.currentPage = _starredChannelsCollectionView.contentOffset.x / SCREEN_WIDTH;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return _homeChannelsArray.count;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    HomeChannels *homeChannel = _homeChannelsArray[indexPath.row];
    if ([homeChannel.type isEqualToString:@"all"]) {
        
        AllStarredChannelsCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"allChannelsCollectionViewCellReuse" forIndexPath:indexPath];
        cell.homeChannel = homeChannel;
        
#warning 找到对应的cell

        return cell;
        
        
    } else {
        
        StarredChannelsCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"homeCollectionViewCellReuse" forIndexPath:indexPath];
        cell.homeChannel = homeChannel;
        cell.backgroundColor = [UIColor whiteColor];
        return cell;
    }
    
}
// 赋值
- (void)setHomeChannelsArray:(NSArray<HomeChannels *> *)homeChannelsArray {
    _homeChannelsArray = homeChannelsArray;
    if (_homeChannelsArray.count >= 4) {
        [self createPageControl];
        _pageControl.numberOfPages = 2;
    }
    [_starredChannelsCollectionView reloadData];
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    double itemCount = _homeChannelsArray.count;
    if (itemCount <= 3) {
        CGSize itemSize = CGSizeMake(self.starredChannelsCollectionView.bounds.size.width - 20, 60);
        return itemSize;
    } else {
        CGSize itemSize = CGSizeMake(self.starredChannelsCollectionView.bounds.size.width - 20, 60);
        return itemSize;
    }
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    HomeChannels *homeChannel = _homeChannelsArray[indexPath.row];
    if ([homeChannel.type isEqualToString:@"all"]) {
        NSLog(@"已经关注");
        UINavigationController *nav = [UIApplication sharedApplication].keyWindow.rootViewController.childViewControllers[0];
        StarredChannelsViewController *starredChannelsVC = [[StarredChannelsViewController alloc] init];
        [nav pushViewController: starredChannelsVC animated:YES];
    } else {
        NSLog(@"个别页面");
        UINavigationController *nav = [UIApplication sharedApplication].keyWindow.rootViewController.childViewControllers[0];
        ChannelsDetailViewController *channelDetailVC = [[ChannelsDetailViewController alloc] init];
//        channelDetailVC.tabBarController.tabBar.hidden = YES;
        channelDetailVC.hidesBottomBarWhenPushed = YES;
//        nav.viewControllers.firstObject.hidesBottomBarWhenPushed = YES;
        channelDetailVC.channelID = _homeChannelsArray[indexPath.row].ID;
        [nav pushViewController:channelDetailVC animated:YES];
        
    }
}


- (void)layoutSubviews {
    [super layoutSubviews];
    
    _layout.sectionInset = UIEdgeInsetsMake(0, 10, 0, 10);
    _starredChannelsCollectionView.contentSize = CGSizeMake(self.contentView.bounds.size.width * 2, self.contentView.bounds.size.height);
    if (_homeChannelsArray.count <= 3) {
        _starredChannelsCollectionView.frame = CGRectMake(0, 0, self.contentView.bounds.size.width, self.contentView.bounds.size.height);
    } else {
       _starredChannelsCollectionView.frame = CGRectMake(0, 0, self.contentView.bounds.size.width, self.contentView.bounds.size.height - 40);
    }
    
    [_pageControl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_starredChannelsCollectionView.mas_bottom).mas_offset(0);
        make.centerX.mas_equalTo(self.contentView.bounds.size.width).mas_offset(0);
        make.width.mas_equalTo(40);
        make.height.mas_equalTo(40);
    }];
}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
